package sfsoftware.util.map;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Vector;
import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.xml.parsers.*;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

public class MapEditor extends JFrame implements ActionListener, MouseListener, MouseMotionListener {
	private final int EDITOR_X = 280;
	private final int EDITOR_Y = 120; //150;
	private final Timer MAIN_TIMER  = new Timer(15, this);
	private Graphics graphicsBuffer;
	private JLabel tileInformation = new JLabel("Tile information");
	private JLabel currentToolLabel = new JLabel("Walkability");
	private JLabel xLabel = new JLabel("X:");
	private JLabel yLabel = new JLabel("Y:");
	private JTextField roomXField = new JTextField(64);
	private JTextField roomYField = new JTextField(64);
	private JPanel panel = new JPanel();
	private JComboBox currentTexture = null;
	private String[] tools = {"Walkability", "Layer 0", "Layer 1"};
	private static String[] approvedTextures = {"tiles1", "objects","dungeon","underground"};
	private JComboBox currentTool = new JComboBox(tools);
	private int currentlySelectedTool = 0;
	private JButton clearLayerButton = new JButton("Clear Selected");
	private JButton clearAllLayerButton = new JButton("Clear All");
	private JButton fillWalkabilityButton = new JButton("Fill Non-Walkability");
	private int brush = 0;
	private static HashMap<String, BufferedImage> textures = new HashMap<String, BufferedImage>();
	private static BufferedImage bimg = null;
	private static final String WINDOW_TITLE = "Trevor Legacy Map Editor";
	private static boolean[][] tileWalkable = new boolean[16][10];
	private static int[][][] tileId = new int[2][16][10];
	private static int[][][] tileSrcX = new int[2][16][10];
	private static int[][][] tileSrcY = new int[2][16][10];
	private static String[][][] tileTexture = new String[2][16][10];
	private static String[] layerTexture = new String[2];
	private static int currentLayer = 0;
	private static File currentMapFile = null;
	private static int currentTileId = 0;
	private static int currentTileSrcX = 0;
	private static int currentTileSrcY = 0;
	private Vector<Actor> objects = new Vector<Actor>();
	
	/**
	 * @throws HeadlessException
	 */
	public MapEditor() throws HeadlessException {
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

		clearLayerButton.setLocation(12, 48);
		clearLayerButton.setSize(120, 25);
		clearLayerButton.setToolTipText("Clears the currently selected layer.");
		clearLayerButton.addActionListener(this);
		//add(clearLayerButton);

		clearAllLayerButton.setLocation(140, 48);
		clearAllLayerButton.setSize(100, 25);
		clearAllLayerButton.setToolTipText("Clears all layers on the current map.");
		clearAllLayerButton.addActionListener(this);
		//add(clearAllLayerButton);

		fillWalkabilityButton.setLocation(250, 48);
		fillWalkabilityButton.setSize(145, 25);
		fillWalkabilityButton.setToolTipText("Sets any populated tile in layer 1 to non-walkable.");
		fillWalkabilityButton.addActionListener(this);
		//add(fillWalkabilityButton);

		JLabel currentLayerLabel = new JLabel("Current Layer:");
		currentLayerLabel.setLocation(12, 10);
		currentLayerLabel.setSize(96, 16);
		add(currentLayerLabel);


		currentTool.setLocation(12, 32);
		currentTool.setSize(100,20);
		currentTool.addActionListener(this);
		currentTool.setEnabled(false);
		//add(currentTool);

		currentToolLabel.setLocation(12, 32);
		currentToolLabel.setSize(100, 16);
		add(currentToolLabel);


		JLabel textureLabel = new JLabel("Texture:");
		textureLabel.setLocation(126, 10);
		textureLabel.setSize(64, 16);
		add(textureLabel);

		currentTexture = new JComboBox(loadTextureNames());
		currentTexture.setLocation(126, 32);
		currentTexture.setSize(128,20);
		currentTexture.setEnabled(false);
		currentTexture.addActionListener(this);
		add(currentTexture);

		tileInformation.setLocation(EDITOR_X,  EDITOR_Y + 360); //510
		tileInformation.setSize(256,16);
		add(tileInformation);

		xLabel.setLocation(670, 14);
		xLabel.setSize(20, 22);
		add(xLabel);
		
		roomXField.setLocation(700, 14);
		roomXField.setSize(32,22);
		roomXField.setText("0");
		add(roomXField);

		yLabel.setLocation(720, 14);
		yLabel.setSize(20, 22);
		add(yLabel);		
		
		roomYField.setLocation(750, 14);
		roomYField.setSize(32,22);
		roomYField.setText("0");
		add(roomYField);

		DefaultListModel listModel = new DefaultListModel();
		//listModel.addElement("Poop");
		JList actorList = new JList(listModel);
		actorList.setLocation(EDITOR_X, 30);
		actorList.setSize(100, 100);
		add(actorList);
		
		
		JButton refreshMapButton = new JButton("Refresh");
		refreshMapButton.setLocation(800, 14);
		refreshMapButton.setSize(100, 25);
		refreshMapButton.addActionListener(new ActionListener() {
	    	public void actionPerformed(ActionEvent event)
	    	{
	    		if(currentMapFile != null && currentMapFile.exists())
	    		{
	    			int roomX = Integer.valueOf(roomXField.getText());
	    			int roomY = Integer.valueOf(roomYField.getText());
	    			openMapFile(currentMapFile, roomX, roomY);
	    			repaint();
	    		}
	    	}
	    });
		add(refreshMapButton);

		add(panel);

		addMouseMotionListener(this);

		JMenuBar menubar = new JMenuBar();

		JMenu fileRootMenu = new JMenu("File");
		JMenu mapRootMenu = new JMenu("Map");
		JMenu layerRootMenu = new JMenu("Layer");

		fileRootMenu.setMnemonic(KeyEvent.VK_F);
		mapRootMenu.setMnemonic(KeyEvent.VK_M);
		layerRootMenu.setMnemonic(KeyEvent.VK_L);

		JMenuItem saveMapItem = new JMenuItem("Save Map To File...");
		saveMapItem.setMnemonic(KeyEvent.VK_S);
		saveMapItem.setToolTipText("Saves the current map to a TLM map file");
		saveMapItem.addActionListener(new ActionListener() {
	    	public void actionPerformed(ActionEvent event)
	    	{
	    		int roomX = Integer.valueOf(roomXField.getText());
	    		int roomY = Integer.valueOf(roomYField.getText());
	    		saveMapToFile(roomX, roomY);
	    	}
	    });

	    JMenuItem openMapItem = new JMenuItem("Open Map From File...");
	    openMapItem.setMnemonic(KeyEvent.VK_O);
	    openMapItem.setToolTipText("Opens an existing map from a TLM file in the editor");
	    openMapItem.addActionListener(new ActionListener() {
	    	public void actionPerformed(ActionEvent event)
	    	{
	    		int roomX = Integer.valueOf(roomXField.getText());
	    		int roomY = Integer.valueOf(roomYField.getText());
	    		openMapFile(roomX, roomY);
	    		repaint();
	    	}
	    });

	    JMenuItem displayXMLMenuItem = new JMenuItem("Display XML Output...");
	    displayXMLMenuItem.setMnemonic(KeyEvent.VK_D);
	    displayXMLMenuItem.setToolTipText("Outputs the String containing the Tag");
	    displayXMLMenuItem.addActionListener(new ActionListener() {
	    	public void actionPerformed(ActionEvent event) {
	    		displayTags();
	        }
	    });

	    JMenuItem newActorMenuItem = new JMenuItem("New Actor...");
	    newActorMenuItem.setMnemonic(KeyEvent.VK_A);
	    newActorMenuItem.setToolTipText("Adds a new Actor to the current map");
	    newActorMenuItem.addActionListener(new ActionListener() {
	    	public void actionPerformed(ActionEvent event)
	    	{
	    		newActorAction(null, null);
	    	}	    	
	    });
	    
	    JMenuItem eMenuItem = new JMenuItem("Exit");
	    eMenuItem.setMnemonic(KeyEvent.VK_E);
	    eMenuItem.setToolTipText("Exit application");
	    eMenuItem.addActionListener(new ActionListener() {
	    	public void actionPerformed(ActionEvent event) {
	    		int dialogResult = JOptionPane.showConfirmDialog (null, "Are you sure you want to exit? Any unsaved data will be lost!", "Warning", JOptionPane.YES_NO_OPTION);
	    		if(dialogResult == JOptionPane.YES_OPTION)
	    		{
	    			System.exit(0);
	    		}
	        }
	    });

	    JMenuItem clearSelectedMenuItem = new JMenuItem("Clear Selected Layer");
	    clearSelectedMenuItem.setMnemonic(KeyEvent.VK_C);
	    clearSelectedMenuItem.setToolTipText("Clears the currently selected layer.");
	    clearSelectedMenuItem.addActionListener(new ActionListener() {
	    	public void actionPerformed(ActionEvent event) {
	    		clearLayerAction();
	        }
	    });

	    JMenuItem fillMenuItem = new JMenuItem("Fill Non-Walkability");
	    fillMenuItem.setToolTipText("Sets any populated tile in layer 1 to non-walkable.");
	    fillMenuItem.setMnemonic(KeyEvent.VK_W);
	    fillMenuItem.addActionListener(new ActionListener() {
	    	public void actionPerformed(ActionEvent event)
	    	{
	    		fillWalkabilityAction();
	    	}
	    });

	    final JRadioButtonMenuItem  walkabilityLayerMenuItem = new JRadioButtonMenuItem ("Walkability");
	    walkabilityLayerMenuItem.setSelected(true);
	    walkabilityLayerMenuItem.addActionListener(new ActionListener() {
	    	public void actionPerformed(ActionEvent event) {
	    		setCurrentTool(0);
	        }
	    });

	    JRadioButtonMenuItem  layer0MenuItem = new JRadioButtonMenuItem ("Layer 0");
	    layer0MenuItem.addActionListener(new ActionListener() {
	    	public void actionPerformed(ActionEvent event) {
	    		setCurrentTool(1);
	        }
	    });
	    JRadioButtonMenuItem  layer1MenuItem = new JRadioButtonMenuItem ("Layer 1");
	    layer1MenuItem.addActionListener(new ActionListener() {
	    	public void actionPerformed(ActionEvent event) {
	    		setCurrentTool(2);
	        }
	    });

		JMenuItem newMapItem = new JMenuItem("New Map");
		newMapItem.setMnemonic(KeyEvent.VK_N);
		newMapItem.setToolTipText("Resets the known universe");
		newMapItem.addActionListener(new ActionListener() {
	    	public void actionPerformed(ActionEvent event)
	    	{
	    		walkabilityLayerMenuItem.setSelected(true);
	    		newMapAction();
	    	}
	    });

	    JMenuItem clearAllMenuItem = new JMenuItem("Clear All Layers");
	    clearAllMenuItem.setMnemonic(KeyEvent.VK_A);
	    clearAllMenuItem.setToolTipText("Clears all layers on the current map.");
	    clearAllMenuItem.addActionListener(new ActionListener() {
	    	public void actionPerformed(ActionEvent event)
	    	{
	    		clearAllLayersAction();
	    	}
	    });

	    //fileRootMenu.add(displayXMLMenuItem);
	    fileRootMenu.add(newMapItem);
	    fileRootMenu.addSeparator();
	    fileRootMenu.add(openMapItem);
	    fileRootMenu.add(saveMapItem);
	    fileRootMenu.addSeparator();
	    fileRootMenu.add(eMenuItem);

	    mapRootMenu.add(fillMenuItem);
	    mapRootMenu.add(newActorMenuItem);
	    mapRootMenu.addSeparator();
	    mapRootMenu.add(displayXMLMenuItem);

	    ButtonGroup layerGroup = new ButtonGroup();
	    layerGroup.add(walkabilityLayerMenuItem);
	    layerGroup.add(layer0MenuItem);
	    layerGroup.add(layer1MenuItem);

	    layerRootMenu.add(walkabilityLayerMenuItem);
	    layerRootMenu.add(layer0MenuItem);
	    layerRootMenu.add(layer1MenuItem);
	    layerRootMenu.addSeparator();
	    layerRootMenu.add(clearSelectedMenuItem);
	    layerRootMenu.add(clearAllMenuItem);

	    menubar.add(fileRootMenu);
	    menubar.add(mapRootMenu);
	    menubar.add(layerRootMenu);

	    setJMenuBar(menubar);
		addMouseListener(this);


        this.setTitle(WINDOW_TITLE);
        this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
        this.setSize(950, 565);
        this.setResizable(false);
        this.setLocationRelativeTo(null);

        MAIN_TIMER.start();

        repaint();
        setVisible(true);
	}

	private void displayTags()
	{
		JFrame newFrame = new JFrame("XML Output for Map");
		JTextArea textArea = new JTextArea(5, 10);
		//JScrollPane scrollPane = new JScrollPane(textArea);
		textArea.setEditable(false);

		textArea.append("<walkability id=\"0\">\n\t" + getWalkabilityTag()  + "\n</walkability>\n");
		for(int i=0; i<layerTexture.length; i++)
		{
			textArea.append("<layer id=\"" + i + "\" texture=\"" + layerTexture[i] + "\">\n\t" + getLayerTag(i) + "\n</layer>\n");
		}

		Actor[] c = new Actor[objects.size()];
		objects.toArray(c);	
		
		for(int i = 0; i < c.length; i++)		
		{
			Actor a = (Actor) c[i];			
			String s  = "<scriptedActor height=\"" + a.height + "\" script=\"" + a.scriptFile + "\" srcx=\"" + a.srcX + "\" srcy=\"" + a.srcY + "\" texture=\"friends\" width=\"" + a.width + "\" x=\"" + a.x + "\" y=\"" + a.y + "\"/>";
			textArea.append(s + "\n");
		}
		
		newFrame.add(textArea);
		newFrame.pack();
		newFrame.setSize(525, 280);
        newFrame.setLocationRelativeTo(null);
		newFrame.setVisible(true);
	}

	/**
	 * @param gc
	 */
	public MapEditor(GraphicsConfiguration gc) {
		super(gc);
	}

	/**
	 * @param title
	 * @throws HeadlessException
	 */
	public MapEditor(String title) throws HeadlessException {
		super(title);
	}

	/**
	 * @param title
	 * @param gc
	 */
	public MapEditor(String title, GraphicsConfiguration gc) {
		super(title, gc);
	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
	 */
	@Override
	public void mousePressed(MouseEvent arg0) {
		int x = arg0.getX();
		int y = arg0.getY();

		int toolBarX = 12;
		int toolBarY = EDITOR_Y;
		int id = 1;

		for(int j = 0; j + 40 < 512; j+=40)
		{
			for(int i = 0; i + 40 < EDITOR_X; i+=40)
			{
				if(x >= toolBarX && x <= toolBarX + 40 && y >= toolBarY && y <= toolBarY + 40)
				{
					currentTileId = id;
					currentTileSrcX = i;
					currentTileSrcY = j;										
				}
				toolBarX = toolBarX + 42;
				if(toolBarX + 40 >= EDITOR_X)
				{
					toolBarX = 12;
					toolBarY = toolBarY + 42;
				}
				id++;
			}
		}

		brush = arg0.getButton();

		mouseDragged(arg0);
	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseReleased(MouseEvent arg0) {
		repaint();
	}
	private void newMapAction()
	{
		setCurrentTool(0);
		roomXField.setText("0");
		roomYField.setText("0");

		objects.clear();
		
		for(int i = 0; i < tileWalkable.length; i++)
		{
			for(int j = 0; j < tileWalkable[0].length; j++)
			{
				tileWalkable[i][j] = false;
			}
		}
		for(int i = 0; i < tileId[0].length; i++)
		{
			for(int j = 0; j < tileId[0][0].length; j++)
			{
				tileTexture[0][i][j] = null;
				tileId[0][i][j] = 1;
				tileSrcX[0][i][j] = 0;
				tileSrcY[0][i][j] = 0;

				for(int z=1; z <tileId.length; z++)
				{
					tileTexture[z][i][j] = null;
					tileId[z][i][j] = 0;
					tileSrcX[z][i][j] = 0;
					tileSrcY[z][i][j] = 0;
				}
			}
		}
		repaint();
	}
	private void clearAllLayersAction()
	{
		int dialogResult = JOptionPane.showConfirmDialog (null, "Are you sure you want to clear all layers on this map?\nAny unsaved data will be permanently lost!", "Warning", JOptionPane.YES_NO_OPTION);
		if(dialogResult == JOptionPane.YES_OPTION)
		{
			for(int i = 0; i < tileWalkable.length; i++)
			{
				for(int j = 0; j < tileWalkable[0].length; j++)
				{
					tileWalkable[i][j] = false;
				}
			}
			for(int i = 0; i < tileId[0].length; i++)
			{
				for(int j = 0; j < tileId[0][0].length; j++)
				{
					tileTexture[0][i][j] = "tiles1";
					tileId[0][i][j] = 1;
					tileSrcX[0][i][j] = 0;
					tileSrcY[0][i][j] = 0;

					for(int z=1; z <tileId.length; z++)
					{
						tileTexture[z][i][j] = "";
						tileId[z][i][j] = 0;
						tileSrcX[z][i][j] = 0;
						tileSrcY[z][i][j] = 0;
					}
				}
			}
			repaint();
		}
	}

	private void clearLayerAction()
	{
		String currentLayerName = "Walkability";
		if(currentlySelectedTool > 0)
		{
			switch(currentLayer)
			{
				case 0:
					currentLayerName = "Layer 0";
					break;
				case 1:
					currentLayerName = "Layer 1";
					break;
			}
		}
		int dialogResult = JOptionPane.showConfirmDialog (null, "Are you sure you want to clear the selected layer (" + currentLayerName + ")?\nAny unsaved data will be permanently lost!", "Warning", JOptionPane.YES_NO_OPTION);
		if(dialogResult == JOptionPane.YES_OPTION)
		{
			int xTile = 0;
			int yTile = 0;
			if(currentlySelectedTool == 0)
			{
				for(int i = 0; i < tileWalkable.length; i++)
				{
					for(int j = 0; j < tileWalkable[0].length; j++)
					{
						tileWalkable[i][j] = false;
					}
				}
			}
			else if(currentlySelectedTool == 1)
			{
				for(int i = 0; i < tileId[0].length; i++)
				{
					for(int j = 0; j < tileId[0][0].length; j++)
					{
						tileTexture[currentLayer][i][j] = "tiles1";
						tileId[currentLayer][i][j] = 1;
						tileSrcX[currentLayer][i][j] = 0;
						tileSrcY[currentLayer][i][j] = 0;
					}
				}
			}
			else if(currentlySelectedTool > 1)
			{
				for(int i = 0; i < tileId[0].length; i++)
				{
					for(int j = 0; j < tileId[0][0].length; j++)
					{
						tileTexture[currentLayer][i][j] = "";
						tileId[currentLayer][i][j] = 0;
						tileSrcX[currentLayer][i][j] = 0;
						tileSrcY[currentLayer][i][j] = 0;
					}
				}
			}
			repaint();
		}
	}

	private void fillWalkabilityAction()
	{
		for(int i = 0; i < tileWalkable.length; i++)
		{
			for(int j = 0; j < tileWalkable[0].length; j++)
			{
				if(tileId[1][i][j] > 0)
				{
					tileWalkable[i][j] = true;
				}
				else
				{
					tileWalkable[i][j] = false;
				}
			}
		}
		repaint();
	}

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == clearLayerButton)
		{
			clearLayerAction();
		}
		else if(e.getSource() == clearAllLayerButton)
		{
			clearAllLayersAction();
		}
		else if(e.getSource() == fillWalkabilityButton)
		{
			fillWalkabilityAction();
		}
		else if(e.getSource() == currentTool)
		{
			setCurrentTool(currentTool.getSelectedIndex());
		}
		else if(e.getSource() == currentTexture)
		{
			layerTexture[currentLayer] = currentTexture.getSelectedItem().toString();
			bimg = textures.get(currentTexture.getSelectedItem().toString());
			int width = bimg.getWidth();
			int height = bimg.getHeight();

			if(currentLayer == 0)
			{
				// Fill layer 0 to save time!
				int xTile = 0;
				int yTile = 0;
		    	for(int i = 0; i < 640; i+= 40)
		    	{
		    		for(int j = 0; j < 400; j+= 40)
		    		{
		    			if(tileTexture[currentLayer][xTile][yTile] == null)
		    			{
		    				tileTexture[currentLayer][xTile][yTile] = currentTexture.getSelectedItem().toString();
		    				tileId[currentLayer][xTile][yTile] = 1;
		    				tileSrcX[currentLayer][xTile][yTile] = 0;
		    				tileSrcY[currentLayer][xTile][yTile] = 0;
		    			}
		    			yTile++;
		    		}
		    		yTile = 0;
		    		xTile++;
		    	}
			}

			repaint();
		}

	}

	public void paint(Graphics g) {
    	Image b = createImage(950, 600);
    	graphicsBuffer = b.getGraphics();

    	super.paint(graphicsBuffer);

    	graphicsBuffer.drawLine(EDITOR_X - 10, EDITOR_Y, EDITOR_X - 10, 545);

    	if(bimg != null)
    	{
    		int toolBarX = 12;
    		int toolBarY = EDITOR_Y;
    		int id = 1;

    		for(int j = 0; j <  (int) (bimg.getHeight() / 40); j++)
    		{
    			for(int i = 0; i < (int) (bimg.getWidth() / 40); i++)
    			{
    				graphicsBuffer.drawImage(bimg, toolBarX, toolBarY, toolBarX + 40, toolBarY + 40, i * 40, j * 40, (i * 40) + 40, (j * 40) + 40, this);
    				graphicsBuffer.setColor(Color.red);
    				graphicsBuffer.drawString(String.valueOf(id), toolBarX + 4, toolBarY + 12);
    				//graphicsBuffer.drawString(i * 40 + ", " + j * 40, toolBarX + 10, toolBarY + 22);
    				toolBarX = toolBarX + 42;
    				id++;
    				if(toolBarX + 40 >= EDITOR_X)
    				{
    					toolBarX = 12;
    					toolBarY = toolBarY + 42;
    				}
    			}
    		}
    	}

    	graphicsBuffer.setColor(Color.black);
    	graphicsBuffer.fillRect(EDITOR_X, EDITOR_Y, 640, 400);

    	int xTile = 0;
    	int yTile = 0;
    	for(int i = 0; i < 640; i+= 40)
    	{
    		for(int j = 0; j < 400; j+= 40)
    		{
    			for(int z = 0; z < 2; z++)
    			{
    				if(tileTexture[z][xTile][yTile] != null)
    				{
    					String s = tileTexture[z][xTile][yTile];
    					BufferedImage tileTexture =  textures.get(s);
    					graphicsBuffer.drawImage(tileTexture, EDITOR_X + i, EDITOR_Y + j, EDITOR_X + i + 40, EDITOR_Y + j + 40, tileSrcX[z][xTile][yTile], tileSrcY[z][xTile][yTile], tileSrcX[z][xTile][yTile] + 40, tileSrcY[z][xTile][yTile] + 40, this);
    				}
    			}

    			if(tileWalkable[xTile][yTile])
    			{
    				graphicsBuffer.setColor(Color.red);
    				graphicsBuffer.drawLine(EDITOR_X +i, EDITOR_Y + j, EDITOR_X + i+40, EDITOR_Y + j+40);
    				graphicsBuffer.drawLine(EDITOR_X +i, EDITOR_Y + j + 40, EDITOR_X + i+40, EDITOR_Y + j);
    			}

       			graphicsBuffer.setColor(Color.white);
       			graphicsBuffer.drawRect(EDITOR_X + i, EDITOR_Y + j, 40, 40);
    			yTile++;
    		}
    		yTile = 0;
    		xTile++;
    	}

		Actor[] c = new Actor[objects.size()];
		objects.toArray(c);	
		
		for(int i = 0; i < c.length; i++)		
		{
			Actor a = (Actor) c[i];
			graphicsBuffer.setColor(Color.blue);
			graphicsBuffer.fillRect(EDITOR_X + a.x, EDITOR_Y + a.y, a.width, a.height);
			//graphicsBuffer.drawImage(tileTexture, EDITOR_X + i, EDITOR_Y + j, EDITOR_X + i + 40, EDITOR_Y + j + 40, tileSrcX[z][xTile][yTile], tileSrcY[z][xTile][yTile], tileSrcX[z][xTile][yTile] + 40, tileSrcY[z][xTile][yTile] + 40, this);
		}
    	
    	// Draw the buffer image to the actual image... confusing, no?
        g.drawImage(b, 0, 0, this);
	}

	private static String getLayerTag(int layer)
	{
		String result = "";
		for(int i = 0; i < tileId[0].length; i++)
		{
			for(int j = 0; j < tileId[0][0].length; j++)
			{
				if(i == 0 && j== 0)
				{
					result = result + tileId[layer][i][j];
				}
				else
				{
					result = result + "," + tileId[layer][i][j];
				}
			}
		}
		String defaultLayerTexture = "objects";
		if(layer == 0)
		{
			defaultLayerTexture = "tiles1";
		}
		return result;
		//return "<layer id=\"" + layer + "\" texture=\"" + defaultLayerTexture + "\">\n\t" + result + "\n</layer>";
	}

	private static int getXFromId(String textureName, int tileId)
	{
		BufferedImage texture = textures.get(textureName);
		int width = texture.getWidth();
		int height = texture.getHeight();
		int currentId = 1;

		for(int j = 0; j < (height - 40); j+= 40)
		{
			for(int i = 0; i < (width - 40); i+=40)
			{
				if(currentId == tileId)
				{
					return i;
				}
				currentId++;
			}
		}
		return 0;
	}

	private static int getYFromId(String textureName, int tileId)
	{
		BufferedImage texture = textures.get(textureName);
		int width = texture.getWidth();
		int height = texture.getHeight();
		int currentId = 1;

		for(int j = 0; j < (height - 40); j+= 40)
		{
			for(int i = 0; i < (width - 40); i+=40)
			{
				if(currentId == tileId)
				{
					return j;
				}
				currentId++;
			}
		}
		return 0;
	}

	private static String getWalkabilityTag()
	{
		String result = "";
		for(int i = 0; i < tileWalkable.length; i++)
		{
			for(int j = 0; j < tileWalkable[0].length; j++)
			{
				if(i == 0 && j== 0)
				{
					if(tileWalkable[i][j])
					{
						result = result + "1";
					}
					else
					{
						result = result + "0";
					}
				}
				else
				{
					if(tileWalkable[i][j])
					{
						result = result + ",1";
					}
					else
					{
						result = result + ",0";
					}
				}
			}
		}
		return result;
		//return "<walkability id=\"0\">\n\t" + result + "\n</walkability>";
	}

	private String getScriptFilename()
	{
        JFileChooser choice = new JFileChooser();

        File directory = new File("./res/scripts");
        if(directory.exists())
        {
        	choice.setCurrentDirectory(directory);
        }
        else
        {
        	choice.setCurrentDirectory(new File("."));
        }
        
        choice.setFileSelectionMode(JFileChooser.FILES_ONLY);
        choice.setAcceptAllFileFilterUsed(false);

        //Adding extension filters
        FileNameExtensionFilter filterTML = new FileNameExtensionFilter("Trevor Script file (*.trv)", "trv");
        choice.addChoosableFileFilter(filterTML);

        int option = choice.showDialog(this, "Select");
        if (option == JFileChooser.APPROVE_OPTION)
        {
        	if(choice.getSelectedFile().exists())
        	{
        		return choice.getSelectedFile().getName();
        	}
        }		
        
        return null;
	}
	
	// Creates a new window with GUI fields for creating a new Actor!
	private void newActorAction(Integer x, Integer y)
	{
		final JFrame newFrame = new JFrame("Add Actor");
		
		JLabel actorXLabel = new JLabel("X:");		
		actorXLabel.setLocation(70, 15);
		actorXLabel.setSize(32, 22);
		
		final JTextField actorXField = new JTextField(20);
		actorXField.setLocation(100, 15);
		actorXField.setSize(32, 22);
		if(x != null)
		{
			actorXField.setText(x.toString());
		}
		
		JLabel actorYLabel = new JLabel("Y:");		
		actorYLabel.setLocation(70, 50);
		actorYLabel.setSize(32, 22);
				
		final JTextField actorYField = new JTextField(20);
		actorYField.setLocation(100, 50);
		actorYField.setSize(32, 22);
		if(y != null)
		{
			actorYField.setText(y.toString());
		}
		
		JLabel actorWidthLabel = new JLabel("Width:");		
		actorWidthLabel.setLocation(150, 15);
		actorWidthLabel.setSize(64, 22);
		
		final JTextField actorWidthField = new JTextField(20);
		actorWidthField.setLocation(200, 15);
		actorWidthField.setSize(32, 22);		
		actorWidthField.setText("40");
		
		JLabel actorHeightLabel = new JLabel("Height:");		
		actorHeightLabel.setLocation(150, 50);
		actorHeightLabel.setSize(64, 22);
				
		final JTextField actorHeightField = new JTextField(20);
		actorHeightField.setLocation(200, 50);
		actorHeightField.setSize(32, 22);
		actorHeightField.setText("40");
		
		final JLabel actorSrcXLabel = new JLabel("Source X:");		
		actorSrcXLabel.setLocation(30, 85);
		actorSrcXLabel.setSize(55, 22);
		
		final JTextField actorSrcXField = new JTextField(20);
		actorSrcXField.setLocation(100, 85);
		actorSrcXField.setSize(32, 22);		
		actorSrcXField.setText("0");
		
		final JLabel actorSrcYLabel = new JLabel("Source Y:");		
		actorSrcYLabel.setLocation(30, 120);
		actorSrcYLabel.setSize(55, 22);		
		
		final JTextField actorSrcYField = new JTextField(20);
		actorSrcYField.setLocation(100, 120);
		actorSrcYField.setSize(32, 22);	
		actorSrcYField.setText("0");
		
		final JTextField assocScriptFile = new JTextField(255);
		assocScriptFile.setLocation(100, 155);
		assocScriptFile.setSize(150, 22);
		
		JButton scriptFileButton = new JButton("...");		
		scriptFileButton.setSize(22, 22);
		scriptFileButton.setLocation(250, 155);
		scriptFileButton.addActionListener(new ActionListener() {
	    	public void actionPerformed(ActionEvent event)
	    	{
	    		assocScriptFile.setText(getScriptFilename());
	    	}
	    });		
		
		JButton okButton = new JButton("OK");		
		okButton.setSize(100, 28);
		okButton.setLocation(40, 200);
		okButton.addActionListener(new ActionListener() {
	    	public void actionPerformed(ActionEvent event)
	    	{
	    		int x = Integer.valueOf(actorXField.getText());
	    		int y = Integer.valueOf(actorYField.getText());
	    		
	    		int srcX = Integer.valueOf(actorSrcXField.getText());
	    		int srcY = Integer.valueOf(actorSrcYField.getText());
	    		
	    		int width = Integer.valueOf(actorWidthField.getText());
	    		int height = Integer.valueOf(actorHeightField.getText());
	    		
	    		String script = assocScriptFile.getText();	    		
	    		objects.add(new Actor(x, y, srcX, srcY, width, height, script));
	    		
	    		newFrame.setVisible(false);
	    		newFrame.dispose();
	    		repaint();
	    	}
	    });
		
		JButton cancelButton = new JButton("Cancel");
		cancelButton.setSize(100, 28);
		cancelButton.setLocation(160, 200);
		cancelButton.addActionListener(new ActionListener() {
	    	public void actionPerformed(ActionEvent event)
	    	{
	    		newFrame.setVisible(false);
	    		newFrame.dispose();
	    	}
	    });
		
		newFrame.getContentPane().setLayout(null);
		newFrame.add(actorXLabel);
		newFrame.add(actorXField);
		newFrame.add(actorYLabel);
		newFrame.add(actorYField);
		newFrame.add(actorWidthLabel);
		newFrame.add(actorHeightLabel);
		newFrame.add(actorWidthField);
		newFrame.add(actorHeightField);
		newFrame.add(actorSrcXLabel);
		newFrame.add(actorSrcXField);
		newFrame.add(actorSrcYLabel);
		newFrame.add(actorSrcYField);
		newFrame.add(assocScriptFile);
		newFrame.add(scriptFileButton);
		newFrame.add(okButton);
		newFrame.add(cancelButton);
		
		newFrame.pack();
		newFrame.setSize(300, 280);
        newFrame.setResizable(false);
		newFrame.setLocationRelativeTo(null);
		newFrame.setVisible(true);		
	}
	
	private void openMapFile(int roomX, int roomY)
	{
        JFileChooser choice = new JFileChooser();

        File directory = new File("./res");
        if(directory.exists())
        {
        	choice.setCurrentDirectory(directory);
        }
        else
        {
        	choice.setCurrentDirectory(new File("."));
        }        
        
        choice.setFileSelectionMode(JFileChooser.FILES_ONLY);
        choice.setAcceptAllFileFilterUsed(false);

        //Adding extension filters
        FileNameExtensionFilter filterTML = new FileNameExtensionFilter("Trevor Legacy map file (*.tlm)", "tlm");
        choice.addChoosableFileFilter(filterTML);

        int option = choice.showOpenDialog(this);
        if (option == JFileChooser.APPROVE_OPTION)
        {
        	if(choice.getSelectedFile().exists())
        	{
        		openMapFile(choice.getSelectedFile(), roomX, roomY);
        	}
        }
	}

	private void openMapFile(File f, int roomX, int roomY)
	{
		try
		{
        	DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = builderFactory.newDocumentBuilder();
		    Document document = builder.parse(new FileInputStream(f));

			String expression = "/map/room[@x='" + roomX + "' and @y='" + roomY + "']/walkability[@id='0']";
			Node walkability = (MapReader.getNodeListFromXPath(expression)).item(0);
			String walkabilityData = walkability.getTextContent().trim();
			String[] walkabilityDatum = walkabilityData.split(",");

			int z = 0;
			for(int xTile = 0; xTile < tileId[0].length; xTile++)
			{
				for(int yTile = 0; yTile < tileId[0][0].length; yTile++)
				{
					if(z < walkabilityDatum.length)
					{
						if(Integer.valueOf(walkabilityDatum[z]) == 1)
						{
							tileWalkable[xTile][yTile] = true;
						}
						else if(Integer.valueOf(walkabilityDatum[z]) == 0)
						{
							tileWalkable[xTile][yTile] = false;
						}
					}
					z++;
				}
			}

			for(int layer = 0; layer < tileId.length; layer++)
			{
				z = 0;
				expression = "/map/room[@x='" + roomX + "' and @y='" + roomY + "']/layer[@id='" + layer + "']";
				NodeList nodeList = MapReader.getNodeListFromXPath(expression);
				String layerTexture = MapReader.getTagAttributeValue(nodeList.item(0),"texture");
				String layerData = nodeList.item(0).getTextContent().trim();
				String[] tileDatum = layerData.split(",");
				for(int xTile = 0; xTile < tileId[0].length; xTile++)
				{
					for(int yTile = 0; yTile < tileId[0][0].length; yTile++)
					{
						tileTexture[layer][xTile][yTile] = null;
						tileId[layer][xTile][yTile] = 0;
						tileSrcX[layer][xTile][yTile] = 0;
						tileSrcY[layer][xTile][yTile] = 0;

						if(z < tileDatum.length)
						{
							if(Integer.valueOf(tileDatum[z]) > 0)
							{
								tileTexture[layer][xTile][yTile] = layerTexture;
								tileId[layer][xTile][yTile] = Integer.valueOf(tileDatum[z]);
								tileSrcX[layer][xTile][yTile] = getXFromId(layerTexture, Integer.valueOf(tileDatum[z]));
								tileSrcY[layer][xTile][yTile] = getYFromId(layerTexture, Integer.valueOf(tileDatum[z]));
							}
						}
						z++;
					}
				}
			}
			this.setTitle(WINDOW_TITLE + " - " + f.getName() + " (" + roomX + ", " + roomY + ")");
			currentMapFile = f;
		}
		catch (ParserConfigurationException e)
		{
		    e.printStackTrace();
		}
		catch (SAXException e)
		{
		    e.printStackTrace();
		}
		catch (IOException e)
		{
		    e.printStackTrace();
		}
		catch (XPathExpressionException e)
		{
			e.printStackTrace();
		}
	}

	private void saveMapToFile(int roomX, int roomY)
	{
        JFileChooser choice = new JFileChooser();

        File directory = new File("./res");
        if(directory.exists())
        {
        	choice.setCurrentDirectory(directory);
        }
        else
        {
        	choice.setCurrentDirectory(new File("."));
        }         
        
        choice.setFileSelectionMode(JFileChooser.FILES_ONLY);
        choice.setAcceptAllFileFilterUsed(false);

        //Adding extension filters
        FileNameExtensionFilter filterTML = new FileNameExtensionFilter("Trevor Legacy map file (*.tlm)", "tlm");
        choice.addChoosableFileFilter(filterTML);

        int option = choice.showSaveDialog(this);
        if (option == JFileChooser.APPROVE_OPTION)
        {
        	String filePath = choice.getSelectedFile().toString();
        	if(!(filePath.endsWith(".tlm")) && !(filePath.endsWith(".TLM")))
        	{
        		filePath = filePath + ".tlm";
        	}

        	currentMapFile = choice.getSelectedFile();
        	this.setTitle(WINDOW_TITLE + " - " + currentMapFile.getName() + " (" + roomX + ", " + roomY + ")");

    		try
    		{
    			DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    			if(choice.getSelectedFile().exists())
    			{
    				// File exists
    				Document doc = docBuilder.parse(filePath);

    				Node rootElement = doc.getFirstChild();


                    String expression = "/map/room[@x='" + roomX + "' and @y='" + roomY + "']";
                    XPath xPath =  XPathFactory.newInstance().newXPath();
                    try
                    {
                            NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET);
                            Node node = nodeList.item(0);
                            if(node != null)
                            {
                            	// If a room already exists, modify the existing tags!
                	    		int dialogResult = JOptionPane.showConfirmDialog (null, "It appears the room specified (" + roomX + ", " + roomY + ") already exists in the destination file! Do you want to proceed?", "Warning", JOptionPane.YES_NO_OPTION);
                	    		if(dialogResult == JOptionPane.YES_OPTION)
                	    		{

                    				expression = "/map/room[@x='" + roomX + "' and @y='" + roomY + "']/walkability[@id='0']";
                                    xPath =  XPathFactory.newInstance().newXPath();
                                    Node n = ((NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET)).item(0);
                                    n.setTextContent(getWalkabilityTag());

                    				for(int i = 0; i < tileId.length; i++)
                    				{
                                        expression = "/map/room[@x='" + roomX + "' and @y='" + roomY + "']/layer[@id='" + i + "']";
                                        xPath =  XPathFactory.newInstance().newXPath();

                                        n = ((NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET)).item(0);
                                        NamedNodeMap attr = n.getAttributes();
                                        Node a = attr.getNamedItem("texture");
                                        a.setTextContent(layerTexture[i]);

                                        n.setTextContent(getLayerTag(i));
                    				}
                	    		}
                            }
                            else
                            {
                				// If room does not exist in the file, append it!
                				Element room = doc.createElement("room");
                				rootElement.appendChild(room);

                				room.setAttribute("x", String.valueOf(roomX));
                				room.setAttribute("y", String.valueOf(roomY));

                				Element walkability = doc.createElement("walkability");
                				room.appendChild(walkability);
                				walkability.setAttribute("id", "0");
                				walkability.appendChild(doc.createTextNode(getWalkabilityTag()));

                				for(int i = 0; i < tileId.length; i++)
                				{
                					Element layer = doc.createElement("layer");
                					room.appendChild(layer);

                					layer.setAttribute("id", String.valueOf(i));
                					layer.setAttribute("texture", layerTexture[i]);
                					layer.appendChild(doc.createTextNode(getLayerTag(i)));
                				}
                            }
                    }
                    catch (XPathExpressionException e)
                    {
                           e.printStackTrace();
                    }


    				TransformerFactory transformerFactory = TransformerFactory.newInstance();
    				Transformer transformer = transformerFactory.newTransformer();
    				DOMSource source = new DOMSource(doc);
    				StreamResult result = new StreamResult(new File(filePath));
    				transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    				transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    				transformer.transform(source, result);
    			}
    			else
    			{
    				// Create new file!
    				Document doc = docBuilder.newDocument();

    				Element rootElement = doc.createElement("map");
    				doc.appendChild(rootElement);

    				Element room = doc.createElement("room");
    				rootElement.appendChild(room);

    				room.setAttribute("x", String.valueOf(roomX));
    				room.setAttribute("y", String.valueOf(roomY));

    				Element walkability = doc.createElement("walkability");
    				room.appendChild(walkability);

    				walkability.setAttribute("id", "0");
    				walkability.appendChild(doc.createTextNode(getWalkabilityTag()));

    				for(int i = 0; i < tileId.length; i++)
    				{
    					Element layer = doc.createElement("layer");
    					room.appendChild(layer);

    					layer.setAttribute("id", String.valueOf(i));
    					layer.setAttribute("texture", layerTexture[i]);
    					layer.appendChild(doc.createTextNode(getLayerTag(i)));
    				}

    				TransformerFactory transformerFactory = TransformerFactory.newInstance();
    				Transformer transformer = transformerFactory.newTransformer();
    				DOMSource source = new DOMSource(doc);
    				StreamResult result = new StreamResult(new File(filePath));
    				transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    				transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    				transformer.transform(source, result);
    			}
    		}
        	catch (ParserConfigurationException pce) {
        		pce.printStackTrace();
        	}
    		catch (TransformerException tfe) {
    			tfe.printStackTrace();
    		}
    		catch (IOException ioe) {
    			ioe.printStackTrace();
    		}
    		catch (SAXException sae) {
    			sae.printStackTrace();
    		}
        }
	}

	private static String[] loadTextureNames()
	{
		ArrayList<String> al = new ArrayList<String>();
		String path = sfsoftware.util.graphics.Graphics.GRAPHICS_FILE_PATH;
		String files;

		File folder = new File(path);
		File[] listOfFiles = folder.listFiles();

		if(listOfFiles != null)
		{
		   for (int i = 0; i < listOfFiles.length; i++)
		   {
			    if (listOfFiles[i].isFile())
			    {
			    	files = listOfFiles[i].getName();
			        if (files.endsWith(".png") || files.endsWith(".PNG"))
			        {
			        	String textureName = files.substring(0, files.indexOf("."));
			        	if(Arrays.asList(approvedTextures).contains(textureName))
			        	{
				        	al.add(textureName);
				        	try
				        	{
								textures.put(textureName, ImageIO.read(listOfFiles[i]));
							}
				        	catch (IOException e)
				        	{
								e.printStackTrace();
							}
			        	}
			        }
			     }
		   }
		}

		String[] stockArr = new String[al.size()];
		stockArr = al.toArray(stockArr);
		return stockArr;
	}

	private void setCurrentTool(int selection)
	{
		currentlySelectedTool = selection;
		if(selection > 0)
		{
			currentTexture.setEnabled(true);
			currentLayer = selection - 1;
			fillWalkabilityButton.setEnabled(false);
		}
		else
		{
			currentTexture.setEnabled(false);
			fillWalkabilityButton.setEnabled(true);
		}

		currentToolLabel.setText(tools[currentlySelectedTool]);
		repaint();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new MapEditor();
	}

	@Override
	public void mouseDragged(MouseEvent arg0) {
		int x = arg0.getX();
		int y = arg0.getY();

		if(x >= EDITOR_X && x <= (EDITOR_X + 640) && y >= EDITOR_Y && y <= (EDITOR_Y + 400))
		{
			x = arg0.getX() - EDITOR_X;
			y = arg0.getY() - EDITOR_Y;

			int xTile = (int) Math.floor(x / 40);
			int yTile = (int) Math.floor(y / 40);

			// Place with left button, remove with right button
			if(brush == 1)
			{
				if(currentlySelectedTool == 0)
				{
					tileWalkable[xTile][yTile] = true;
				}
				else
				{
					tileTexture[currentLayer][xTile][yTile] = currentTexture.getSelectedItem().toString();
					tileId[currentLayer][xTile][yTile] = currentTileId;
					tileSrcX[currentLayer][xTile][yTile] = currentTileSrcX;
					tileSrcY[currentLayer][xTile][yTile] = currentTileSrcY;
				}
			}
			else
			{
				if(currentlySelectedTool == 0)
				{
					tileWalkable[xTile][yTile] = false;
				}
				else
				{
					tileTexture[currentLayer][xTile][yTile] = null;
					tileId[currentLayer][xTile][yTile] = 0;
					tileSrcX[currentLayer][xTile][yTile] = 0;
					tileSrcY[currentLayer][xTile][yTile] = 0;
				}
			}

			repaint();
		}
	}

	@Override
	public void mouseMoved(MouseEvent arg0) {
		int x = arg0.getX();
		int y = arg0.getY();

		if(x >= EDITOR_X && x <= (EDITOR_X + 640) && y >= EDITOR_Y && y <= (EDITOR_Y + 400))
		{
			x = arg0.getX() - EDITOR_X;
			y = arg0.getY() - EDITOR_Y;

			int xTile = (int) Math.floor(x / 40);
			int yTile = (int) Math.floor(y / 40);

			if(xTile >= 0 && yTile >= 0)
			{
				tileInformation.setText("Tile " + xTile + ", " + yTile + "         X, Y: " + (xTile*40) + ", " + (yTile* 40));
			}
		}
	}

	// Private inner class to handle storing "Actors" on the map in the map editor.
	private class Actor 
	{
		public int x;
		public int y;
		public int srcX = 0;
		public int srcY = 0;
		public int width = 40;
		public int height = 40;
		public String scriptFile;
		
		private Actor(int destX, int destY, int destSrcX, int destSrcY, int destWidth, int destHeight, String associatedScript)
		{
			x = destX;
			y = destY;
			srcX = destSrcX;
			srcY = destSrcY;
			width = destWidth;
			height = destHeight;
			scriptFile = associatedScript;
			System.out.println("Actor created!");
		}
	}	
}